import * as dotenv from 'dotenv'
import express from 'express'
import jwt from 'jsonwebtoken'

dotenv.config({
  path: ['.env.local', '.env']
})

const accessKeyId = process.env.ACCESS_KEY_ID // import.meta.env.VITE_ACCESS_KEY_ID
const accessKeySecret = process.env.ACCESS_KEY_SECRET

const app = express()
const port = 3000

// 生成 token
const authKlingai = async () => {
  const headers = {
    algorithm: 'HS256'  // 签名算法
  }
  const now = Math.floor(Date.now() / 1000) // 当前时间戳
  const payload = {
    iss: accessKeyId,
    exp: now + 1800,
    nbf: now - 5
  }
  const token = jwt.sign(payload, accessKeySecret, headers)
  return token
}


app.get('/jwt-auth', async (req, res) => {
  const token = await authKlingai()
  res.end(token)
})


app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})